package com.project.structures.json;

import java.util.ArrayList;
import java.util.List;

import com.project.bean.Features;
import com.project.bean.Function;

public class FeaturesMap {
	
	private List<Features> features;

	/**
	 * @return the features
	 */
	public List<Features> getFeatures() {
		return features;
	}
	
	/**
	 * @param features the features to set
	 */
	public void setFeatures(List<Features> features) {
		this.features = features;
	}
	
	@Override
	public String toString() {
		String result = "";
		result = "{\n\t\"features\" : [";
		String strFea = "";
		for(Features feature : features) {
			strFea =  !"".equals(strFea) ? strFea + "," : strFea;
			strFea += "\n\t\t{" +
					  "\n\t\t\t\"featureId\" : \"" + feature.getFeatureId() +"\"," +
					  "\n\t\t\t\"featureLink\" : \"" + feature.getFeatureLink() +"\"," +
					  "\n\t\t\t\"featureName\": \"" + feature.getFeatureName() + "\"," +
					  "\n\t\t\t\"featureDescription\": \"" + feature.getFeatureDescription() + "\"," +
					  "\n\t\t\t\"active\": " + feature.getActive() + "," +
					  "\n\t\t\t\"functions\":[";
			String strFun = "";
			for(Function function : feature.getFunctions()) {
				strFun = !"".equals(strFun) ? strFun + "}," : strFun;
				strFun += "{\"name\" : \"" + function.getName() + "\"";
			}
			strFea = strFea + strFun + "}]\n\t\t}";
		}	
		result = result + strFea + "\n\t]";
		result = result + "\n};";
		
		return result;
	}
	
	public void addFeature(Features feature) {
		for(Features f : features) {
			if (f.getFeatureLink().equalsIgnoreCase(feature.getFeatureLink())) {
				features.remove(f);
				break;
			}
		}

		features.add(feature);
	}
	
	public void addAllFeature(List<Features> feature) {
		if (features == null) {
			features = new ArrayList<Features>();
		}
		features.addAll(feature);
	}
}
